package com.fansl.koala.quickdev.module.auth.controller;

import com.fansl.koala.quickdev.common.annation.DecryptParam;
import com.fansl.koala.quickdev.common.bean.R;
import com.fansl.koala.quickdev.common.bean.po.SysLoginPo;
import com.fansl.koala.quickdev.common.bean.po.SysUserRegisterPo;
import com.fansl.koala.quickdev.common.bean.vo.LoginVo;
import com.fansl.koala.quickdev.module.auth.service.IAuthService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

/**
 * @author fansl
 * @Description: 权限相关-控制器
 * @date 2020/3/13 15:04
 */
@RestController
@AllArgsConstructor
@RequestMapping("/auth")
public class AuthController {
    private final IAuthService authService;

    /**
     * 系统用户注册
     * @param param
     * @return
     */
    @PostMapping("/register")
    public Mono<R<String>> sysUserRegister(@RequestBody SysUserRegisterPo param) {
        //TODO 验证手机验证码
        //TODO 密码参数解密
        Boolean result = authService.registerSysUser(param);
        return Mono.just(R.ok(result?"注册成功":"注册失败"));
    }

    /**
     * 系统用户注册激活
     * @param username 用户名
     * @return code 激活码
     */
    @GetMapping("/activate")
    public Mono<R<String>> sysUserActivate(@RequestParam String username,
                                           @RequestParam String code) {
        Boolean result = authService.activateSysUser(username,code);
        return Mono.just(R.ok(result?"激活成功":"激活成功"));
    }

    /**
     * 系统用户重新发送激活码
     * @param username 用户名
     * @return code 激活码
     */
    @GetMapping("/reactivate")
    public Mono<R<String>> sysUserReactivate(@RequestParam String username) {
        Boolean result = authService.sysUserReactivate(username);
        return Mono.just(R.ok(result?"发送成功":"发送失败"));
    }

    @PostMapping("/sysLogin")
    @DecryptParam
    public Mono<R<LoginVo>> sysUserLogin(@RequestBody SysLoginPo param){
        LoginVo result = authService.sysLogin(param);
        return Mono.just(R.ok(result));
    }
}
